package com.jiaquyun.jcyx.entity

import android.content.Context
import com.contrarywind.interfaces.IPickerViewData
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.module.lemlin.http.HttpResponseData
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.Serializable

data class AddressDetailsResponse(
    val data: AddressDetails
) : HttpResponseData()

data class AddressDetails(
    val address: String,
    val area: String,
    val city: Int,
    val consignee: String,
    val district: Int,
    val full_address: String,
    val gender: Int,
    val id: Int,
    val is_default: Boolean,
    val lat: String,
    val lng: String,
    val mobile: String,
    val province: Int
) : Serializable

class AddressSave {
    var id: String? = null
    var consignee: String? = null
    var gender: String? = null
    var mobile: String? = null
    var is_default: String? = null
    var address: String? = null
    var addressIndexes: String? = null
    var lng: String? = "103.999779"
    var lat: String? = "35.957603"
    //116.435889,39.9125
}

fun getProvinceList(context: Context): List<ProvinceEntity> {
    val builder = StringBuilder()
    val bufferedReader = BufferedReader(InputStreamReader(context.assets.open("region.json")))
    var content: String?
    while (bufferedReader.readLine().also { content = it } != null) {
        builder.append(content)
    }
    bufferedReader.close()
    val provinceArray = Gson().fromJson(builder.toString(), Array<ProvinceEntity>::class.java)
    return provinceArray.asList()
}

fun getProvinceUIList(provinceList: List<ProvinceEntity>): ProvinceUIData {
    val cityList = mutableListOf<List<CityEntity>>()
    val areaListList = mutableListOf<List<List<AreaEntity>>>()
    for (province in provinceList) {
        val areaList = mutableListOf<List<AreaEntity>>()
        cityList.add(province.cityList)
        for (city in province.cityList) {
            areaList.add(city.areaList)
        }
        areaListList.add(areaList)
    }
    return ProvinceUIData(provinceList, cityList, areaListList)
}

data class ProvinceEntity(
    val provinceId: Int,
    val provinceName: String = "",
    @SerializedName("citys")
    val cityList: MutableList<CityEntity> = mutableListOf()
) : IPickerViewData {
    override fun getPickerViewText(): String = provinceName
}

data class CityEntity(
    val cityId: Int,
    val cityName: String = "",
    @SerializedName("areas")
    val areaList: MutableList<AreaEntity> = mutableListOf()
) : IPickerViewData {
    override fun getPickerViewText(): String = cityName
}

data class AreaEntity(
    val id: Int,
    val areaName: String = ""
) : IPickerViewData {
    override fun getPickerViewText(): String = areaName
}

data class ProvinceUIData(
    val provinceUIList: List<ProvinceEntity>,
    val cityUIList: List<List<CityEntity>>,
    val areaUIList: List<List<List<AreaEntity>>>
)